'use strict';

const { BaseParameter, stateKeys, addCommentSchema, CommentQuery } = require('../core/base_mongo_model');

module.exports = app => {
  const mongoose = app.mongoose;
  const Schema = mongoose.Schema;

  const commentQuery = CommentQuery(Schema)

  const studySchema = new Schema({
    ...BaseParameter,
    /**
     * 标题
     */
    title: { type: String, default: '' },
    /**
     * 时间
     */
    date: { type: Date, default: new Date() },
    /**
     * 类型
     */
    type: { type: String, default: '' },
    /**
     * 内容
     */
    content: { type: String, default: '' },
    /**
     * 描述
     */
    describe: { type: String, default: '' },
    /**
     * 图片链接
     */
    pic: { type: String, default: '' },
    // 评论
    ...commentQuery
  });

  // eslint-disable-next-line jsdoc/require-param
  /**
   * 获取所有类型
   */
  studySchema.statics.findTypes = function() {
    return this.distinct('type', {
      $or: [
        { stateKey: stateKeys.NORMAL },
        { stateKey: { $exists: false } },
      ],
    });
  };

  // eslint-disable-next-line jsdoc/require-param
  /**
   * 获取查询参数
   */
  studySchema.statics.getParmas = function(data) {
    const params = {
      $and: [
        {
          $or: [
            { stateKey: stateKeys.NORMAL },
            { stateKey: { $exists: false } },
          ],
        },
      ],
    };
    if (data.type !== 'all') {
      params.type = data.type;
    }
    if (data.searchKey) {
      const reg = { $regex: `${data.searchKey}`, $options: 'i' };
      // eslint-disable-next-line default-case
      switch (data.searchType) {
        case 0:
          params.$and.push({
            $or: [
              { title: reg },
              { describe: reg },
              { content: reg },
            ]
          })
          break;
        case 1:
          params.title = reg;
          break;
        case 2:
          params.describe = reg;
          break;
        case 3:
          params.content = reg;
          break;
      }
    }
    return params;
  };

  // eslint-disable-next-line jsdoc/require-param
  /**
   * 获取列表数据
   */
  studySchema.statics.findList = function(data) {
    const params = this.getParmas(data);
    const skip = (data.pageNum - 1) * data.pageSize;
    const model = this.find(params, 'title date type describe pic stateKey').skip(skip).limit(data.pageSize);
    model.sort({ date: -1 });
    return model.exec();
  };

  // eslint-disable-next-line jsdoc/require-param
  /**
   * 获取个数
   */
  studySchema.statics.findCount = function(data) {
    const params = this.getParmas(data);
    return this.countDocuments(params);
  };

  // eslint-disable-next-line jsdoc/require-param
  /**
   * 获取单个的详情信息
   */
  studySchema.statics.findDetail = function(id) {
    return this.findOne({
      _id: mongoose.Types.ObjectId(id),
    }, 'title date type content describe pic').populate({
      path: 'commentsList',
      options: { sort: '-date' },
      populate: {
        path: 'children',
        options: { sort: '-date' },
      },
    });
  };

  // eslint-disable-next-line jsdoc/require-param
  /**
   * 更新数据
   */
  studySchema.statics.updateModel = function(id, upData) {
    return this.updateOne({
      _id: mongoose.Types.ObjectId(id),
    }, upData);
  };

  // eslint-disable-next-line jsdoc/require-param
  /**
   * 删除单个数据
   * @param id 要删除数据的ID
   * @param thoroughly 是否测地删除数据
   * @return { Promise }
   */
  studySchema.statics.removeModel = function(id, thoroughly = false) {
    if (thoroughly) {
      return this.deleteOne({
        _id: mongoose.Types.ObjectId(id),
      });
    }
    return this.updateOne({
      _id: mongoose.Types.ObjectId(id),
    }, {
      stateKey: stateKeys.DUSTBIN,
    });
  };

  // eslint-disable-next-line jsdoc/require-param
  /**
   * 从回收站还原
   */
  studySchema.statics.dustbinRestore = function(id) {
    return this.updateOne({
      _id: mongoose.Types.ObjectId(id),
    }, {
      stateKey: stateKeys.NORMAL,
    });
  };

  addCommentSchema(studySchema, mongoose)

  return mongoose.model('Study', studySchema, 'studys');
};
